<template>
  <div class="register-container">
    <div class="particle-container">
      <div v-for="i in 50" :key="i" class="particle"></div>
    </div>
    <div class="cyber-circle"></div>
    <div class="cyber-circle circle-2"></div>
  
    <div class="content">
      <div class="login-box">
      <div class="head">
          <div class="title-container">
            <h1>账号登录</h1>
            <div class="cyber-line"></div>
          </div>
          <div class="back-link" @click="$router.go(-1)">
            返回
            <span class="cyber-arrow">←</span>
          </div>
      </div>

        <div class="login-methods">
          <button @click="selectEmail" :class="{ active: isEmailLogin }" class="cyber-button">邮箱登录</button>
          <button @click="selectCode" :class="{ active: isCodeLogin }" class="cyber-button">验证码登录</button>
      </div>
  
      <!-- 验证码登录 -->
        <el-form ref="phoneForm" :model="formData" :rules="codeRules" v-if="isCodeLogin">
          <div class="cyber-input-group">
            <p>邮箱号</p>
            <el-form-item prop="email">
              <el-input v-model="formData.email" placeholder="请输入邮箱号"></el-input>
              <div class="cyber-line"></div>
        </el-form-item>
          </div>
          
          <div class="cyber-input-group">
            <p>验证码</p>
            <el-form-item prop="code">
              <div class="code-input">
                <el-input v-model="formData.code" placeholder="请输入验证码"></el-input>
                <button @click="getCode()" :disabled="isCodeDisabled" class="cyber-button code-button">
                  <span class="button-text">{{ codeText }}</span>
                  <div class="button-glitch"></div>
                </button>
              </div>
              <div class="cyber-line"></div>
        </el-form-item>
          </div>

          <el-form-item class="remember-me">
          <el-checkbox v-model="formData.rememberMe">记住我</el-checkbox>
        </el-form-item>

          <el-form-item>
            <button @click="submitCode()" class="cyber-button submit-button" type="button">
              <span class="button-text">登 录</span>
              <div class="button-glitch"></div>
            </button>
        </el-form-item>
      </el-form>

      <!-- 邮箱密码登录 -->
      <el-form ref="emailForm" :model="formData" :rules="emailRules" v-if="isEmailLogin">
          <div class="cyber-input-group">
            <p>邮箱</p>
            <el-form-item prop="email">
              <el-input v-model="formData.email" placeholder="请输入邮箱"></el-input>
              <div class="cyber-line"></div>
        </el-form-item>
          </div>

          <div class="cyber-input-group">
            <p>密码</p>
            <el-form-item prop="password">
              <el-input type="password" v-model="formData.password" placeholder="请输入密码"></el-input>
              <div class="cyber-line"></div>
        </el-form-item>
          </div>

          <el-form-item class="remember-me">
            <el-checkbox v-model="formData.rememberMe">记住我</el-checkbox>
        </el-form-item>

          <el-form-item>
            <button @click="submitEmail()" class="cyber-button submit-button" type="button">
              <span class="button-text">登 录</span>
              <div class="button-glitch"></div>
            </button>
        </el-form-item>
      </el-form>
      
        <div class="bottom-links">
          <router-link to="/register" class="cyber-link">还没有账号？立即注册</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '../config/http';


export default {
  components:{
    Header: () => import("@/components/Header.vue")
  },
  data(){
    return{
      formData:{
        email:'',
        password:'',
        code:'',
        rememberMe:true,
        type:2,
        username:'123'
      },
      // 控制不同的方式登录
      isEmailLogin:false,   // 邮箱密码
      isCodeLogin:true,
      // 获取验证码
      isCodeDisabled:false,
      codeText: '获取验证码',
        countdown: 60,
        timer: null,
     
      // 验证码登录表单正则
      codeRules: {
        email:[{required:true, message:'请输入正确的邮箱号',trigger: 'blur'},
        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        code:{ required: true, message: '请输入验证码', trigger: 'blur' }
      },
      // 邮箱密码登录表单正则
      emailRules:{
        email:[{required:true, message:'请输入正确的邮箱号',trigger: 'blur'},
        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        password:[{required:true,message:'请输入密码',trigger:'blur'},
        {min:3,max:12,message:'输入长度在3-12',trigger:'blur'}]
      }
      
    }
  },
  methods:{
    selectCode(){
      this.isEmailLogin=false
      this.isCodeLogin=true
      this.formData.type = 2
    },
    selectEmail(){
     this.isEmailLogin=true
     this.isCodeLogin=false
     this.formData.type = 1
    },
  
    // 提交按钮
 
    // 验证码登录
    submitCode(){
      this.$refs.phoneForm.validate(async (isOk)=>{
        if(isOk){
          const result = await axios.post('/as/accounts/login',this.formData
        )
          // console.log(result.data);
          if(result.data.data){
            this.$store.dispatch('user/login',result.data.data)
            this.$router.push('/')
            this.getUserInfo()
          }else{
            this.$message(result.data.msg)
          }
          
        }else{
          this.$message.error('验证码错误！');
        }
      })
    },

    // 密码登录
    submitEmail(){
      this.$refs.emailForm.validate(async(isOk)=>{
        if(isOk){
          const result = await axios.post('/as/accounts/login',this.formData)
          console.log(result.data);
          this.$message.error(result.data.msg)
          if(result.data.data){
            this.$store.dispatch('user/login',result.data.data)
            this.$message.success('登录成功！')
            this.$router.push('/')
            this.getUserInfo()
          }

        }else{
          this.$message.error('邮箱或密码错误！');
        }
      })
    },
    // 获取验证码
    async getCode(){
      if(!this.formData.email){
        this.$message('请输入邮箱！')
        return
      }
      if(!/^[-a-zA-Z0-9_.+]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/.test(this.formData.email)){
      this.$message('邮箱格式不正确！')
      return
      }
      this.isCodeDisabled=true
      this.startCountdown()
      let result = await axios.post('/as/accounts/captcha',{
        email:this.formData.email
      },
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
      
    },
    startCountdown(){
      if(this.timer){
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.countdown--
        this.codeText = `${this.countdown}秒后重新获取`
        if(this.countdown === 0){
          this.countdown = 60
          this.codeText = '获取验证码'
          this.isCodeDisabled = false
          clearInterval(this.timer)
        }
      }, 1000);
    },

    // 获取当前登录用户信息
    async getUserInfo(){
      let result = await axios.get('/us/user/current')
      console.log(result.data);
      const userInfo = result.data.data.data
      const {email, password,status,type,updateTime,username} = userInfo
      console.log(email,password);
      
      this.$store.dispatch('user/handleUserInfo',{email, password,status,type,updateTime,username})
    }

    

  }
}
</script>

<style lang="less" scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    width: 200%;
    height: 200%;
    top: -50%;
    left: -50%;
    background: radial-gradient(circle at center, transparent 0%, #000 70%),
                repeating-linear-gradient(transparent 0%, transparent 2px, rgba(0, 255, 157, 0.1) 3px, transparent 3px, transparent 9px),
                repeating-linear-gradient(90deg, transparent 0%, transparent 2px, rgba(0, 255, 157, 0.1) 3px, transparent 3px, transparent 9px);
    animation: matrix 20s linear infinite;
    opacity: 0.2;
  }

  &::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, #00ff9d, transparent);
    animation: scan 3s linear infinite;
  }

  .particle-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
    
    .particle {
      position: absolute;
      width: 2px;
      height: 2px;
      background: #00ff9d;
      border-radius: 50%;
      opacity: 0.5;
      box-shadow: 0 0 10px #00ff9d, 0 0 20px #00ff9d;

      .particle-position(1, 10%, 20%, 0.2s, 4s);
      .particle-position(2, 30%, 50%, 0.4s, 4.5s);
      .particle-position(3, 45%, 70%, 0.6s, 5s);
      .particle-position(4, 60%, 25%, 0.8s, 4.2s);
      .particle-position(5, 75%, 35%, 1s, 4.8s);
      .particle-position(6, 85%, 80%, 1.2s, 5.2s);
      .particle-position(7, 20%, 85%, 1.4s, 4.3s);
      .particle-position(8, 35%, 15%, 1.6s, 4.7s);
      .particle-position(9, 50%, 45%, 1.8s, 5.1s);
      .particle-position(10, 65%, 65%, 2s, 4.4s);
      .particle-position(11, 80%, 90%, 2.2s, 4.9s);
      .particle-position(12, 15%, 75%, 2.4s, 5.3s);
      .particle-position(13, 40%, 30%, 2.6s, 4.1s);
      .particle-position(14, 55%, 60%, 2.8s, 4.6s);
      .particle-position(15, 70%, 40%, 3s, 5s);
      .particle-position(16, 90%, 10%, 3.2s, 4.5s);
      .particle-position(17, 25%, 95%, 3.4s, 4.8s);
      .particle-position(18, 95%, 55%, 3.6s, 5.2s);
      .particle-position(19, 5%, 85%, 3.8s, 4.3s);
      .particle-position(20, 45%, 5%, 4s, 4.7s);
    }
  }

  .particle-position(@index, @left, @top, @delay, @duration) {
    &:nth-child(@{index}) {
      left: @left;
      top: @top;
      animation: float @duration infinite @delay, glow 2s infinite alternate @delay;
    }
  }

  .cyber-circle {
    position: absolute;
    width: 600px;
    height: 600px;
    border: 2px solid rgba(0, 255, 157, 0.1);
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    animation: rotate 20s linear infinite;
    
    &::after {
      content: '';
      position: absolute;
      width: 100%;
      height: 100%;
      border: 2px solid rgba(0, 255, 157, 0.2);
      border-radius: 50%;
      animation: pulse 2s ease-out infinite;
    }

    &::before {
      content: '';
      position: absolute;
      width: 120%;
      height: 120%;
      border: 1px solid rgba(0, 255, 157, 0.1);
      border-radius: 50%;
      animation: pulse 2s ease-out infinite 1s;
    }

    &.circle-2 {
      width: 800px;
      height: 800px;
      animation-direction: reverse;
      animation-duration: 30s;
    }
  }

  .content {
    position: relative;
    z-index: 1;
    width: 530px;
    padding: 40px;
  }

  .login-box {
    background: rgba(13, 17, 23, 0.85);
    border-radius: 12px;
    box-shadow: 0 0 30px rgba(0, 255, 157, 0.2),
                inset 0 0 15px rgba(0, 255, 157, 0.1);
    backdrop-filter: blur(20px);
    border: 1px solid rgba(0, 255, 157, 0.3);
    padding: 20px;
    animation: contentGlow 3s infinite alternate;

    &::before {
      content: '';
      position: absolute;
      top: -2px;
      left: -2px;
      right: -2px;
      bottom: -2px;
      background: linear-gradient(45deg, #00ff9d, transparent, #00ff9d, transparent);
      border-radius: 12px;
      z-index: -1;
      animation: borderGlow 3s linear infinite;
    }

    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 35px;

      .title-container {
        h1 {
          font-size: 26px;
          color: #fff;
          margin: 0;
          text-shadow: 0 0 10px rgba(0, 255, 157, 0.5);
          animation: textGlow 2s infinite alternate;
        }

        .cyber-line {
          height: 2px;
          background: linear-gradient(90deg, transparent, #00ff9d, transparent);
          margin-top: 8px;
          animation: scan 2s linear infinite;
        }
      }

      .back-link {
        color: #00ff9d;
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 5px;
        font-size: 14px;
        
        .cyber-arrow {
          animation: blink 1s infinite;
        }

        &:hover {
          text-shadow: 0 0 10px #00ff9d;
        }
      }
    }

    .login-methods {
      display: flex;
      justify-content: center;
      gap: 20px;
      margin-bottom: 35px;

      .cyber-button {
        background: transparent;
        border: 1px solid rgba(0, 255, 157, 0.3);
        color: #fff;
        padding: 10px 25px;
        font-size: 14px;
        cursor: pointer;
        transition: all 0.3s;

        &.active {
          background: rgba(0, 255, 157, 0.1);
          border-color: #00ff9d;
          box-shadow: 0 0 15px rgba(0, 255, 157, 0.3);
        }

        &:hover {
          border-color: #00ff9d;
          box-shadow: 0 0 15px rgba(0, 255, 157, 0.3);
        }
      }
    }

    .cyber-input-group {
      margin-bottom: 28px;

      p {
        color: #00ff9d;
        margin-bottom: 8px;
        font-size: 14px;
      }

  .el-input {
      .el-input__inner {
          background: rgba(0, 255, 157, 0.05);
          border: 1px solid rgba(0, 255, 157, 0.2);
          color: #fff;
          height: 42px;

          &:focus {
            border-color: #00ff9d;
            box-shadow: 0 0 15px rgba(0, 255, 157, 0.2);
          }
        }
      }

      .cyber-line {
        height: 1px;
        background: linear-gradient(90deg, transparent, rgba(0, 255, 157, 0.3), transparent);
        margin-top: 4px;
      }
    }

    .code-input {
      display: flex;
      gap: 10px;

      .el-input {
        flex: 1;
      }

      .code-button {
        width: 120px;
        position: relative;
        overflow: hidden;
      }
    }

    .remember-me {
      margin: 25px 0;

      .el-checkbox {
        color: rgba(255, 255, 255, 0.7);
      }
    }

    .submit-button {
      width: 100%;
      height: 46px;
      background: transparent;
      border: 1px solid #00ff9d;
      color: #fff;
      font-size: 16px;
      position: relative;
      overflow: hidden;
      margin-top: 25px;
      
      &::before {
        content: '';
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(0, 255, 157, 0.2), transparent);
        animation: btnGlow 3s infinite;
      }

      &:hover {
        background: rgba(0, 255, 157, 0.1);
        box-shadow: 0 0 20px rgba(0, 255, 157, 0.4);
      }

      .button-glitch {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(0, 255, 157, 0.2), transparent);
        transform: translateX(-100%);
      }
    }

    .bottom-links {
      text-align: center;
      margin-top: 25px;

      .cyber-link {
        color: #00ff9d;
        text-decoration: none;
        font-size: 14px;
        position: relative;
        
        &::after {
          content: '';
          position: absolute;
          bottom: -2px;
          left: 0;
          width: 100%;
          height: 1px;
          background: #00ff9d;
          transform: scaleX(0);
          transition: transform 0.3s;
        }

        &:hover::after {
          transform: scaleX(1);
        }
      }
    }
  }
}

@keyframes matrix {
  0% {
    transform: translateY(0) rotate(0deg);
  }
  100% {
    transform: translateY(-50%) rotate(45deg);
  }
}

@keyframes scan {
  0% {
    transform: translateX(-100%);
    opacity: 0;
  }
  50% {
    transform: translateX(0);
    opacity: 1;
  }
  100% {
    transform: translateX(100%);
    opacity: 0;
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-20px);
  }
}

@keyframes glow {
  0% {
    box-shadow: 0 0 5px #00ff9d;
  }
  100% {
    box-shadow: 0 0 20px #00ff9d, 0 0 30px #00ff9d;
  }
}

@keyframes rotate {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(1.2);
    opacity: 0;
  }
}

@keyframes contentGlow {
  0% {
    box-shadow: 0 0 20px rgba(0, 255, 157, 0.2);
  }
  100% {
    box-shadow: 0 0 40px rgba(0, 255, 157, 0.4);
  }
}

@keyframes borderGlow {
  0% {
    opacity: 0;
    transform: rotate(0deg);
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: rotate(360deg);
  }
}

@keyframes textGlow {
  0% {
    text-shadow: 0 0 5px rgba(0, 255, 157, 0.5);
  }
  100% {
    text-shadow: 0 0 15px rgba(0, 255, 157, 0.8);
  }
}

@keyframes btnGlow {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

@keyframes blink {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

@media screen and (max-width: 768px) {
  .register-container {
    .content {
      width: 90%;
      margin: 20px;
      padding: 20px;
    }

    .cyber-circle {
      display: none;
      }
    }
}
</style>